require(['../js/appConf','../js/bsConfig', '../js/duties','../js/bsPlugins'], function() {
    // Configuration loaded now, safe to do other require calls
    // that depend on that config.
    
    require(['init','dataTable','dataTables-jqueryui','jqueryValidate','domReady!'],
    	function(init,dataTable,ui,validate,doc) {
    		
    		
    		var funcDef = {
    			/**
    	 * 初始化列表
    	 * @param _options.module	模块代码
    	 * @param _options.ajax		server端数据访问方法
    	 * @param _options.paging	分页标志
    	 * @param _options.columns	缺省列
    	 */
    	initList:function(_options){
    		
    		 
    		var _default = {
    			processing: true,
    			serverSide: true,
    			//searching: false,
    			 "lengthMenu": [[10, 15, 25, 50, -1], [10, 15, 25, 50, "All"]],
    			rootid: null,
    			gridSelector: 'dg-list',
    			method: 'getListDomain',    			
    			//paging:true,
    			bPaginate: true,
    			//pagingType: "full_numbers",
    			pageLength: 15,
    			//sDom:'lfrtip',//default layout
    			sDom:'tilp',
    			rowId:'id',
    			columns: [
    				{"data":null,"title":"序号"},
    				{ "data": "bizno","title":"业务编码"},
					//{ "事项类型": "bizname" },
					{ "data": "name" ,"title":"事项名称"},
					{ "data":  "apply_deptname","title":"申请科室"},
					{ "data":  "apply_name","title":"申请人员"},
					{ "data": "apply_date" ,"title":"申请时间"}
					//{ "当前环节": "status" },
					//{ "附件": "archive" }
    			],
    			
    			"oLanguage": {//下面是一些汉语翻译
                    "sProcessing":   "处理中...",
				    "sLengthMenu":   "每页 _MENU_ 条记录",
				    "sZeroRecords":  "没有匹配结果",
				    "sInfo":         "第 _START_ 至 _END_ 条，共 _TOTAL_ 条",
				    "sInfoEmpty":    "显示第 0 至 0 条，共 0 条",
				    "sInfoFiltered": "(由 _MAX_ 条结果过滤)",
				    "sInfoPostFix":  "",
				    "sSearch":       "搜索:",
				    "sUrl":          "",
				    "sEmptyTable":     "表中数据为空",
				    "sLoadingRecords": "载入中...",
				    "sInfoThousands":  ",",
				    "oPaginate": {
				        "sFirst":    "首页",
				        "sPrevious": "上页",
				        "sNext":     "下页",
				        "sLast":     "末页"
				    },
				    "oAria": {
				        "sSortAscending":  ": 以升序排列此列",
				        "sSortDescending": ": 以降序排列此列"
				    }
                }
                ,"columnDefs": [ {
		            "searchable": false,
		            "orderable": false,
		            "targets": 0
		        } ],
		        
		        //"order": [[ 1, 'asc' ]]//定义初始排序事件，触发列的回写事件
                "fnDrawCallback": function(){
				　　var api = this.api();
				　　var startIndex= api.context[0]._iDisplayStart;//获取到本页开始的条数
				　　api.column(0).nodes().each(function(cell, i) {
				　　　　cell.innerHTML = startIndex + i + 1;
				　　}); 
				}
				,
				"rowCallback": function( row, data ) {
		            if ( $.inArray(data.DT_RowId, selected) !== -1 ) {
		                $(row).addClass('selected');
		            }
		        }
                //"iDisplayLength": 10//每页显示10条数据
                
                //"bAutoWidth": true //宽度是否自动，感觉不好使的时候关掉试试
    		}
    		
    		
    		var def = $.extend({},_default,_options);
    		
    		if(_options.module){
    			var _m = _options.module;
    			_options.module = 'bs'+_m.substring(2)
    			if(window.console){
    				console.log('module code: ' + _options.module);
    			}
    		}
    		//_options.module = 'tempDrugPurchase';
    		if(_options && _options.module){
    			def.ajax = {
    				url:'/'+init.getSite()+'/'+_options.module+'/query/'+def.method+'.action',
    				
    				type:'post'    			
    			}
    			if(dg_table){
    				dg_table.destroy();
    			}
    			dg_table =$('#'+rootid + ' table.'+def.gridSelector).DataTable(def); 
    			
    			dg_table.on('click', 'tr', function () {
			        var id = this.id;
			        var index = $.inArray(id, selected);			        
			 
			        if ( index === -1 ) {
			            selected.push( id );
			        } else {
			            selected.splice( index, 1 );
			        }
			 		
			        $(this).toggleClass('selected');
			        
			        
			        //funcDef.tableClick({bizcode:_options.module})
			        
			        funcDef.tableClick({bizcode:_options.module,id:id})
			    } );
    		}
    	}
    		}
  	})
  	
})